package com.wpx.Service.Impl;

import com.wpx.Mapper.ArticleMapper;
import com.wpx.Mapper.ParagraphMapper;
import com.wpx.Mapper.WordMapper;
import com.wpx.Pojo.Article;
import com.wpx.Pojo.Paragraph;
import com.wpx.Pojo.Word;
import com.wpx.Service.WordService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class WordServcieImpl implements WordService {

    @Autowired
    public WordMapper wordMapper;
    @Autowired
    public ParagraphMapper paragraphMapper;
    @Autowired
    public ArticleMapper articleMapper;

    @Override
    public List<List<Word>> getwordlist(String content) {
        String[] split = content.split("\n");
        List<String> sList = new ArrayList<>();
        for (String string : split) {
            if (string == null || string.equals("")) {
                continue;
            }
            sList.add(string);
        }
        List<List<Word>> llw = new ArrayList<>();
        for (String string : sList) {
            List<Word> lw = new ArrayList<>();
            for (int i = 0; i < string.length(); i++) {
                char c = string.charAt(i);
                if(c==' '){
                    continue;
                }
                Word w = new Word();
                w.setWord(c + "");
                lw.add(w);
            }
            llw.add(lw);

        }
        return llw;
    }

    @Override
    public List<List<Word>> getwordlistById(Integer articleId) {
        Article article= articleMapper.getById(articleId);
        List<Paragraph> paragraphList = paragraphMapper.getParagraphList(articleId);
        List<List<Word>> llw=new ArrayList<>();
        for (Paragraph paragraph : paragraphList) {
            List<Word>lw= wordMapper.getWordById(paragraph.getId());
            llw.add(lw);
        }
        return llw;
    }

    @Override
    public void updatePinyin(Word word) {
        wordMapper.updatePinyin(word.getId(),word.getPinyin());
    }

    @Override
    public void updateBold(Word word) {
        if (word.getIsBold() == 0) {
            word.setIsBold(1);
        } else {
            word.setIsBold(0);
        }
        wordMapper.updateBold(word.getId(),word.getIsBold());

    }
}
